/**
 * Copyright (c) 2022 Presto Labs Pte. Ltd.
 * Author: Ju
 */

#include <glog/logging.h>
#include <gtest/gtest.h>

#include "coin2/base/packer.h"
#include "coin2/base/unpacker.h"

using namespace coin2::base;

TEST(PackerTester, SimplePack) {
  Packer pkr;
  pkr.PushUint32(1);
  pkr.PushUint64(2);
  pkr.PushUint16(0);
  pkr.PushUint8(0);

  std::string str("abcde");
  pkr.PushString(str);

  pkr.Pack();

  uint32_t length = 4 + 4 + 8 + 2 + 1 + 4 + str.size();
  EXPECT_EQ(length, pkr.Length());

  const char *buf = pkr.Buffer();
  EXPECT_EQ(length, *reinterpret_cast<const uint32_t *>(buf));

  Unpacker unpkr(buf, length);
  EXPECT_EQ(unpkr.PopUint32(), 1);
  EXPECT_EQ(unpkr.PopUint64(), 2);
  EXPECT_EQ(unpkr.PopUint16(), 0);
  EXPECT_EQ(unpkr.PopUint8(), 0);

  EXPECT_EQ(unpkr.PopString(), str);
}